//Copyright 2008 by Wes Corp for Trafficman  All rights reserved.abstract 

package org.trafficman.trafficman_main;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.io.FileNotFoundException;

public class TrafficmanFilesLocationNotesDbAdapter {

	public static final String KEY_ROWID = "_id";
	public static final String KEY_DEVICE = "device";
	public static final String KEY_SELECTED_LOC = "selected_location";
	public static final String KEY_JV_DATETIME_CREATED = "datetime_record_created";
	public static final String KEY_JV_DATETIME_UPDATED = "datetime_record_updated";


//	private static final String DATABASE_CREATE = "create table notes (_id integer primary key autoincrement, "
//			+ "title text not null, body text not null);";
	
	private static final String DATABASE_CREATE = "create table if not exists devices"
			+ TrafficmanConstants.DV_VER
			+ " (_id integer primary key autoincrement, device text not null, selected_location integer, datetime_record_created text not null, datetime_record_updated text not null)";

	private SQLiteDatabase mDb;
	private final Context mCtx;
	private boolean return_code;
	private String str_Active_Device;
	private int int_col_index;
	private long lng_Active_Device;


	public TrafficmanFilesLocationNotesDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public TrafficmanFilesLocationNotesDbAdapter open() throws SQLException {
		try {
			mDb = mCtx.openDatabase(TrafficmanConstants.DATABASE_DEVICES, null);
		} catch (FileNotFoundException e) {
			try {
				mDb = mCtx.createDatabase(TrafficmanConstants.DATABASE_DEVICES,
						TrafficmanConstants.DATABASE_VERSION, 0, null);
				mDb.execSQL(DATABASE_CREATE);
			} catch (FileNotFoundException e1) {
				throw new SQLException("Could not create database");
			}
		}
		return this;
	}

	public void close() {
		mDb.close();
	}

	public long createNote(String device, int selected_location, String datetime_record_created, String datetime_record_updated) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_DEVICE, device);
		initialValues.put(KEY_SELECTED_LOC, selected_location);
		initialValues.put(KEY_JV_DATETIME_CREATED, datetime_record_created);
		initialValues.put(KEY_JV_DATETIME_UPDATED, datetime_record_updated);
		return mDb.insert(TrafficmanConstants.DATABASE_TABLE_DEVICE_NAME, null,
				initialValues);
	}

	public boolean deleteNote(long rowId) {
		return mDb.delete(TrafficmanConstants.DATABASE_TABLE_DEVICE_NAME, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public Cursor fetchAllNotes() {
		return mDb.query(TrafficmanConstants.DATABASE_TABLE_DEVICE_NAME, new String[] { KEY_ROWID, KEY_DEVICE, KEY_SELECTED_LOC, KEY_JV_DATETIME_CREATED, KEY_JV_DATETIME_UPDATED }, null, null, null, null, null);
	}

	public Cursor fetchNote(long rowId) throws SQLException {
		Cursor result = mDb.query(true, TrafficmanConstants.DATABASE_TABLE_DEVICE_NAME, new String[] {
				KEY_ROWID, KEY_DEVICE, KEY_SELECTED_LOC, KEY_JV_DATETIME_CREATED, KEY_JV_DATETIME_UPDATED }, KEY_ROWID + "=" + rowId,
				null, null, null, null);
		if ((result.count() == 0) || !result.first()) {
			throw new SQLException("No note matching ID: " + rowId);
		}
		return result;
	}

	public boolean updateNote(long rowId, String device, int selected_location, String datetime_record_created, String datetime_record_updated) {
		ContentValues args = new ContentValues();
		args.put(KEY_DEVICE, device);
		args.put(KEY_SELECTED_LOC, selected_location);
		args.put(KEY_JV_DATETIME_CREATED, datetime_record_created);
		args.put(KEY_JV_DATETIME_UPDATED, datetime_record_updated);
		return mDb.update(TrafficmanConstants.DATABASE_TABLE_DEVICE_NAME, args, KEY_ROWID + "=" + rowId,
				null) > 0;
	}

	public void fetch_Active_Storage_Location() {
		String str_sql = "SELECT " + KEY_ROWID + "," + KEY_DEVICE + "," + KEY_SELECTED_LOC + " FROM " + TrafficmanConstants.DATABASE_TABLE_DEVICE_NAME + " WHERE selected_location=\"1\";";
		Cursor result = mDb.rawQuery(str_sql, null);
		return_code = result.first();
		if (return_code == true) {
			int_col_index = result.getColumnIndex("_id");
			TrafficmanConstants.ACTIVE_DEVICE_ID = result.getLong(int_col_index);
			int_col_index = result.getColumnIndex("device");
			TrafficmanConstants.ACTIVE_DEVICE = result.getString(int_col_index);
		} else {
			TrafficmanConstants.ACTIVE_DEVICE_ID = 0;
			TrafficmanConstants.ACTIVE_DEVICE = "";
		}
		return;
	}

	public Cursor set_Android_Storage_Active(String device) {
		
		String str_sql = "SELECT " + KEY_ROWID + "," + KEY_DEVICE + "," + KEY_SELECTED_LOC + " FROM " + TrafficmanConstants.DATABASE_TABLE_DEVICE_NAME + " WHERE device=\"" + device + "\"";
		Cursor result = mDb.rawQuery(str_sql, null);
		return_code = result.first();
		if (return_code == true) {
			int_col_index = result.getColumnIndex("selected_location");
			result.updateInt(int_col_index, 1);
			result.commitUpdates();
		} else {
			str_Active_Device = "";
		}
		//return;
		return null;
	}
	
}
